﻿using AutoMapper.Internal.Mappers;
using Himp.DataHub;
using Himp.Platform;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Validation;

namespace Himp.ShareData
{
    /// <summary>
    /// 人员信息服务
    /// </summary>
    public class PsnInfoService : PlatformAppService, IPsnInfoService
    {
        private readonly IRepository<PsnInfo, Guid> _psnInfoRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="psnInfoRepository"></param>
        /// <param name="deptmentRepository"></param>
        public PsnInfoService(IRepository<PsnInfo, Guid> psnInfoRepository)
        {
            _psnInfoRepository = psnInfoRepository;
        }

        /// <summary>
        /// 获取人员列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PsnInfoDto>> GetListAsync(PsnInfoInput input)
        {
            var iQuery = await _psnInfoRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(input.DeptId != null, d => d.DeptId == input.DeptId);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<PsnInfo>, List<PsnInfoDto>>(iQuery.ToList());

                return new PagedResultDto<PsnInfoDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<PsnInfo>, List<PsnInfoDto>>(items.ToList());

                return new PagedResultDto<PsnInfoDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 根据成员ID获取成员信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PsnInfoDto> GetAsync(Guid id)
        {
            var item = await _psnInfoRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<PsnInfo, PsnInfoDto>(item);

            return itemDto;
        }

        /// <summary>
        /// 添加成员信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PsnInfoDto> PostAsync(PsnInfoCreateDto input)
        {
            var staff = ObjectMapper.Map<PsnInfoCreateDto, PsnInfo>(input);
            staff = await _psnInfoRepository.InsertAsync(staff);
            var itemDto = ObjectMapper.Map<PsnInfo, PsnInfoDto>(staff);

            return itemDto; 
        }

        /// <summary>
        /// 修改成员信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PsnInfoDto> PutAsync(Guid id, PsnInfoUpdateDto input)
        {
            var staff = await _psnInfoRepository.GetAsync(id);
            staff = ObjectMapper.Map(input, staff);
            staff = await _psnInfoRepository.UpdateAsync(staff);
            var itemDto = ObjectMapper.Map<PsnInfo, PsnInfoDto>(staff);

            return itemDto;
        }

        /// <summary>
        /// 人员信息删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _psnInfoRepository.DeleteAsync(id);
        }
    }
}
